<?php
namespace Index\Widget;
use Think\Controller;

class CatePhWidget extends Controller {



    public function index($cate_id = 0,$type=1,$limit=15){
        S(array('type'=>'File','expire'=>79200));
        $cacheName = md5(__METHOD__.$cate_id.$type.$limit);
        $cacheV = S($cacheName);
        if($cacheV){
            return $cacheV;
        }

    	if(is_numeric($cate_id) && $limit > 0 && $type > 0){
    		$variable = $this->getPhData($cate_id,$type,$limit);
    	}
    	//p($cate_id);p($type);
    	if(!is_array($variable)){
    		return false;
    	}
    	$html = '<ul>';
        foreach ($variable as $key => $value) {
        	$html .= '<li><span class="hits">'.date('m-d',$value['ntime']).'</span><span class="num">'.($key+1).'.</span>';
            $html .= '<a href="/'.$value['book_id'].'.html" title="'.$value['book_name'].'" target="_blank">'.$value['book_name'].'</a></li>';
        }
        $html .= '</ul>';
        S($cacheName,$html);
        return $html;
    }

    public function home($cate_id = 0,$type=1,$limit=12){
        S(array('type'=>'File','expire'=>72000));
        $cacheName = md5(__METHOD__.$cate_id.$type.$limit);
        $cacheV = S($cacheName);
        if($cacheV){
            return $cacheV;
        }
        if(is_numeric($cate_id) && $limit > 0 && $type > 0){
            $variable = $this->getPhData($cate_id,$type,$limit);
        }
        //p($cate_id);p($type);
        if(!is_array($variable)){
            return false;
        }
        $html = '<ul>';
        $Author = new \Common\Model\BookAuthorModel();
        foreach ($variable as $key => $value) {
            $author_name = $Author->getAuthorName($value['author_id']);
            $html .= '<li><a href="/'.$value['book_id'].'.html">'.$value['book_name'].'</a> / '.$author_name.'</li>';
        }
        $html .= '</ul>';
        S($cacheName,$html);
        return $html;
    }

    //1:总榜；2:月榜；3:周榜
    protected function getPhData($cate_id=0,$type = '1',$limit=15){
        $BookTj = new \Common\Model\BookTjModel();
        switch ($type) {
            case '1':
                $ids = $BookTj->totalTj($cate_id,$limit);
                break;
            case '2':
                $ids = $BookTj->monthTj($cate_id,$limit);
                break;
            case '3':
                $ids = $BookTj->weekTj($cate_id,$limit);
                break;
        }

        if(is_array($ids)){
        	$Book = new \Common\Model\BookModel();
        	$Article = new \Common\Model\BookArticleModel();
            foreach ($ids as $key => $value) {
                # code...
                $bookinfo = $Book->where("`book_id`='{$value['book_id']}'")->field('book_name,author_id')->find();
                $ids[$key]['book_name'] = $bookinfo['book_name'];
                $ids[$key]['author_id'] = $bookinfo['author_id'];
                $new  = $Article->getNewArticle($value['book_id']);
                $ids[$key]['ntime'] = $new['create_time'];
            }
            return $ids;
        }
    }

}